package zzzhc.image.model;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class RecognizeTestModel extends NormalTableModel {

	private static final long serialVersionUID = -7694608470675831547L;

	private static final String[] COLUMNS = { "图片字符", "图片", "识别结果" };

	private volatile int recognizedCount = 0;

	private volatile int errorCount = 0;

	private long startTestTime;

	public void clear() {
		recognizedCount = 0;
		errorCount = 0;
		startTestTime = 0;
		clearAll();
	}

	public long getStartTestTime() {
		return startTestTime;
	}

	public void setStartTestTime(long startTestTime) {
		this.startTestTime = startTestTime;
	}

	public RecognizeTestModel() {
		init(COLUMNS, new Class[] { String.class, BufferedImage.class,
				String.class }, new Boolean[] { true, false, true });
	}

	public void addColumn(String code, BufferedImage image) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(COLUMNS[0], code);
		map.put(COLUMNS[1], image);
		addRow(map);
	}

	public void setRecognizeResult(int index, String result) {
		setValueAt(result, index, 2);
		recognizedCount++;
		String code = (String) getValueAt(index, 0);
		if (!code.equals(result)) {
			errorCount++;
		}
	}

	public BufferedImage getImage(int index) {
		return (BufferedImage) getValueAt(index, 1);
	}

	public int getRecognizedCount() {
		return recognizedCount;
	}

	public double getSpeed() {
		return recognizedCount * 1.0d
				/ (System.currentTimeMillis() - startTestTime) * 1000;
	}

	public double correctRate() {
		return 1 - errorCount * 1.0d / recognizedCount;
	}

	public int getErrorCount() {
		return errorCount;
	}

}
